package com.scalar.forecast.projectSetup.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.projectSetup.forms.ProjectSetupForm;
import com.scalar.forecast.projectSetup.ProjectSetup;
import com.scalar.forecast.projectSetup.dao.ProjectSetupDAO;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * Processes the request to disable the Project Setup
 *
 * Created by Veeru. 
 * Date: Feb 19, 2005
 * Time: 3:59:34 PM 
 */

public class ProjectSetupDeleteCommand extends ActionCommand {
    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) {

        ActionForward actionForward = mapping.findForward( "projectSearch" );
        ProjectSetupForm projectSetupForm = ( ProjectSetupForm )form;
        ProjectSetup projectSetup = projectSetupForm.getProjectSetup();
        boolean success = false;
        DBTransaction dbTransaction = null;
        HttpSession session = request.getSession ();

        HashMap errors = new HashMap ();
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        HashMap info = new HashMap ();
        request.setAttribute( SessionKeys.INFO_MESSAGES, info );

        try {
            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();

            ProjectSetupDAO.deleteProjectSetup( dbTransaction, projectSetup.getProjectID()  );
            success = true;

        } catch ( Exception e){
            e.printStackTrace();
        }
        finally {
            if ( success ) {
                try {
                    dbTransaction.commit();
                    info.put( "Project  '"+projectSetup.getProjectName()+"'  deleted successfully", null);
                    if ( mapping.getAttribute () != null )
                        {
                            if ( "request".equals ( mapping.getScope () ) )
                                request.removeAttribute ( mapping.getAttribute () );
                            else
                                session.removeAttribute ( mapping.getAttribute () );
                        }
                }catch ( Exception e){
                    e.printStackTrace();
                }
            }else {
                try {
                    dbTransaction.rollback();
                } catch ( Exception e ){
                    e.printStackTrace();
                }
                errors.put( "Error : Could not delete Project Setup", null);
            }
        }
        return actionForward;
    }
}
